package com.sun.dailyprj.view.canvas;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.Shader;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.sun.dailyprj.R;
import com.sun.dailyprj.util.Logger;

public class InsProgress extends View {

    private static final String TYPE_OVERLAY1 = "overlay1";
    private static final String TYPE_OVERLAY2 = "overlay2";
    private static final String TYPE_OVERLAY3 = "overlay3";
    private String mType = TYPE_OVERLAY1;

    private float height = 120;
    private float width = 240;
    private float offset = 242;
    private final static int BG_Color = 0x66FFFFFF;
    private int sColor = 0xA4826E;
    private int bgColor;
    private int[] mGradientColor = new int[3];

    private final static int sMAX = 100;
    private final static int sMIN = 0;
    private int progress;
    private int mMax;
    private int mMin;

    public InsProgress(Context context) {
        super(context);
    }

    public InsProgress(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initAttrs(context, attrs);
    }

    public InsProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void initAttrs(Context context, AttributeSet attributeSet) {
        TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.CocoView);
        height = typedArray.getDimension(R.styleable.CocoView_height, 5);
        width = typedArray.getDimension(R.styleable.CocoView_first_width, 17);
        offset = typedArray.getDimension(R.styleable.CocoView_offset_width, 3);
        bgColor = typedArray.getColor(R.styleable.CocoView_bg_color, BG_Color);
        sColor = typedArray.getColor(R.styleable.CocoView_first_color, Color.GREEN);
        mType = typedArray.getString(R.styleable.CocoView_pg_type);
        mGradientColor[0] = typedArray.getColor(R.styleable.CocoView_pg_color_start, Color.RED);
        mGradientColor[1] = typedArray.getColor(R.styleable.CocoView_pg_color_center, Color.GRAY);
        mGradientColor[2] = typedArray.getColor(R.styleable.CocoView_pg_color_end, Color.GREEN);
        mMax = typedArray.getInt(R.styleable.CocoView_pg_max, sMAX);
        mMin = typedArray.getInt(R.styleable.CocoView_pg_min, sMIN);
        typedArray.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        if (TYPE_OVERLAY2.equals(mType)) {
            drawOverlay2(canvas, width);
        } else if (TYPE_OVERLAY3.equals(mType)) {
            drawOverlay3(canvas, width);
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                drawOverlay1(canvas, width);
            } else {
                //
                throw new RuntimeException("SDK_INT not support api");
            }
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void drawOverlay1(Canvas canvas, int width) {
        Path path = new Path();
        //绘制pg背景
        canvas.save();
        path.reset();
        Paint paint = new Paint();
        paint.setColor(bgColor);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawRoundRect(0, 0, width, height, offset, offset, paint);
        //更新pg
        paint.setColor(sColor);
        float rate = (float) ((progress * 1.0 * width) / 100);
        float[] position = new float[]{0, 0.5f, 1};
        LinearGradient gradient = new LinearGradient(0, 0, rate, height, mGradientColor, position, Shader.TileMode.CLAMP);
        paint.setShader(gradient);
        canvas.drawRoundRect(0, 0, rate, height, offset, offset, paint);
    }

    private void drawOverlay2(Canvas canvas, int width) {
        Path path = new Path();
        //绘制pg背景
        canvas.save();
        path.reset();
        path.moveTo(offset, 0);
        path.lineTo(0, height);
        path.lineTo(width - offset, height);
        path.lineTo(width, 0);
        canvas.clipPath(path);
        canvas.drawColor(bgColor);
        //绘制pg进度
        canvas.save();
        path.reset();
        float rate = progress * width / 100;
        path.moveTo(offset, 0);
        path.lineTo(0, height);
        path.lineTo(rate, height);
        path.lineTo(rate + offset, 0);

        Paint paint = new Paint();
        float[] position = new float[]{0, 0.5f, 1};
        LinearGradient gradient = new LinearGradient(0, 0, rate, height, mGradientColor, position, Shader.TileMode.CLAMP);
        paint.setShader(gradient);
        canvas.clipPath(path);
        canvas.drawPath(path, paint);
//       canvas.drawColor(sColor);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void drawOverlay3(Canvas canvas, int width) {
        Path path = new Path();
        //绘制pg背景
        canvas.save();
        path.reset();
        Paint paint = new Paint();
        paint.setColor(bgColor);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawRoundRect(0, 0, width, height, offset, offset, paint);

        //更新pg
        paint.setColor(sColor);
        float rate = (float) ((progress * 1.0 * width) / 100);
        float[] position = new float[]{0, 0.5f, 1};
        LinearGradient gradient = new LinearGradient(0, 0, rate, height, mGradientColor, position, Shader.TileMode.CLAMP);
        paint.setShader(gradient);
        Logger.debug("offset = " + offset + " ; rate =" + rate);
        if (rate <= offset) {
//            canvas.drawCircle(offset, offset, offset, paint);
            RectF rect = new RectF(0, (offset -rate ), (int)rate, offset + rate);
            canvas.drawArc(rect, 90, 180, true, paint);

        } else if (offset < rate && rate < (width - offset)) {
//            canvas.drawCircle(offset, offset, offset, paint);
//            canvas.drawArc(0, (offset - rate/2), rate, 2 * rate, 90, 180, false, paint);
            RectF rect = new RectF(0, (offset -rate ), (int)rate, offset + rate);
            canvas.drawArc(rect, 90, 180, false, paint);
            canvas.drawRect(offset, 0, rate, 2 * offset, paint);
        } else {
//            canvas.drawCircle(offset, offset, offset, paint);
//            canvas.drawRect(offset, 0, rate, 2 * offset, paint);
            canvas.drawCircle((width - offset), offset, offset, paint);
        }

    }

    public void setProgress(int pg) {
        if (pg >= sMAX) {
            progress = sMAX;
        } else if (pg <= sMIN) {
            progress = sMIN;
        } else {
            progress = pg;
        }
        postInvalidate();
    }
}
